# -*- coding: utf-8 -*-
# @Author   : robin

import requests
import base64

'''
通用物体和场景识别
'''
def  general_detect(img):
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
    # request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"
    # 二进制方式打开图片文件
    # f = open('static/images/zhiwu.jpg', 'rb')
    # img = base64.b64encode(f.read())
    # print(img)
    params = {"image":img, "baike_num": 20}
    access_token = '24.59b5b3716b1e5b050e998aefadf61767.2592000.1603529392.282335-16179300'
  # '24.b1da061ffeda228123e2de58238b1a17.2592000.1596270059.282335-19463208'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        # print (response.json())
        # print(response.text)
        return response.json()
if __name__ == '__main__':
    # 二进制方式打开图片文件
    f = open('static/images/baocai.jpg', 'rb')
    img = base64.b64encode(f.read())
    print(general_detect(img))